package user

import (
	"Project/global"
	"Project/model/user"
	"Project/model/user/request"
	"Project/utils"
	"errors"
)

type SaleUserService struct{}

func (s *SaleUserService) AddUserSale(saleId int64, userInfo request.UserPersonInfo) error {
	// 获取当前用户所在药店id
	var shopId int64
	if err := global.GVA_DB.Debug().Model(&user.UserSale{}).Select("shop_id").First(&shopId, "sale_id=?", saleId).Error; err != nil {
		return err
	}

	// 创建新的销售人员
	var userSale = user.UserSale{
		SaleID: utils.GenID(),
		ShopID: shopId,
		UserCommon: global.UserCommon{
			UserName: userInfo.UserName,
			AuthID:   8886,
			Card:     userInfo.Card,
			Phone:    userInfo.Phone,
			Password: utils.BcryptHash(userInfo.Password),
			IsUse:    true,
		},
	}
	// 存储
	err := global.GVA_DB.Debug().Create(&userSale).Error
	return err
}

func (s *SaleUserService) GetAllUserSaleByShopId(shopId int64) ([]user.UserSale, error) {
	var userSaleList []user.UserSale
	err := global.GVA_DB.Debug().Model(&user.UserSale{}).Where("shop_id=?", shopId).Find(&userSaleList).Error
	return userSaleList, err
}

func (s *SaleUserService) DeleteUserSale(saleId string) error {
	err := global.GVA_DB.Debug().Model(&user.UserSale{}).Where("sale_id=?", saleId).Delete(&user.UserSale{}).Error
	return err
}

// 根据销售人员ID更新信息
func (s *SaleUserService) UpdateUserSale(u request.UpdateInfo) error {
	// 加密密码
	u.UserInfo.Password = utils.BcryptHash(u.UserInfo.Password)
	// 设置更新信息
	var userSale user.UserSale
	{
		userSale.UserName = u.UserInfo.UserName
		userSale.Card = u.UserInfo.Card
		userSale.Phone = u.UserInfo.Phone
		userSale.Password = u.UserInfo.Password
	}
	// 更新
	err := global.GVA_DB.Debug().Model(&user.UserSale{}).Where("sale_id=?", u.SaleId).Updates(userSale).Error
	return err
}

func (s *SaleUserService) UpdateUserSalePersonalInfo(personalInfo *request.SalePersonalInfo) error {
	err := global.GVA_DB.Debug().Model(&user.UserSale{}).Where("sale_id=?", personalInfo.SaleId).Updates(map[string]interface{}{
		"sale_id":   personalInfo.SaleId,
		"user_name": personalInfo.UserName,
		"phone":     personalInfo.Phone,
		"card":      personalInfo.Card,
	}).Error
	return err
}

func (s *SaleUserService) GetById(saleId int64, info *request.SalePersonalInfo) error {
	err := global.GVA_DB.Debug().Model(&user.UserSale{}).Where("sale_id=?", saleId).First(info).Error
	return err
}

// 根据用户id获取所属医院id
func (s *SaleUserService) GetShopIdBySaleId(saleId int64) (int64, error) {
	var shopId int64
	err := global.GVA_DB.Debug().Model(&user.UserSale{}).Select("shop_id").First(&shopId, "sale_id=?", saleId).Error
	return shopId, err
}

func (s *SaleUserService) GetPwdBySaleId(saleId int64) (pwd string, err error) {
	err = global.GVA_DB.Debug().Model(&user.UserSale{}).Select("password").First(&pwd, "sale_id=?", saleId).Error
	return
}

func (s *SaleUserService) UpdatePassword(saleId int64, password request.PasswordInfo) (err error) {
	// 比对新旧密码是否一致
	if password.OldPassword == password.NewPassword {
		return errors.New("新旧密码相同,请重新修改")
	}
	// 比对新密码和确认密码是否一致
	if password.ConfirmPassword != password.NewPassword {
		return errors.New("两次输入密码不同")
	}
	// 根据id拿到原始密码，比对old密码，看是否一致
	var dbPwd string
	if err = global.GVA_DB.Debug().Model(&user.UserSale{}).Select("password").First(&dbPwd, "sale_id=?", saleId).Error; err != nil {
		return
	}
	if !utils.BcryptCheck(password.OldPassword, dbPwd) {
		return errors.New("原始密码不正确")
	}

	// 更新密码
	if err = global.GVA_DB.Debug().Model(&user.UserSale{}).Where("sale_id=?", saleId).Update("password", utils.BcryptHash(password.NewPassword)).Error; err != nil {
		return err
	}
	return nil
}
